<?php
namespace Cyfy\Modules\Objects;

/**
 * Tabs - bookmarks.
 *
 * The content you can switch between.
 *
 * Example
 * <code>
 *  $tabs = new \Cyfy\Modules\Objects\Tabs();
 *  $tabs -> addTab("tab1", "Tabul8tor 1");
 *  $tabs -> addTab("tab2", "Kniha");
 *  $tabs -> addTab("tab3", "About");
 *  $tabs -> setData("tab1", "Obsah prvniho tabu");
 *  $tabs -> setData("tab2", "Navstevni kniha");
 *  $tabs -> setData("tab3", "O nas, nejaky dlouhy clanek....");
 *  $output .= $tabs -> show();
 */
class Tabs extends GeneralObject
{
    /**
     * List of tabs
     * @var array
     */
    private $tabs = array();

    /**
     * Unique code of identification
     * @var string
     */
    private $unique;
    
    /**
     * Default tab
     * @var array
     */
    private $defaultTab;
  
    /**
     * Creates a new set of tabs.
     */
    function __construct()
    {
        $this -> unique = "tabSet" . uniqid();	
    }
  
    /**
     * Creates a new tab
     * @param string Code - ID - string without white space
     * @param string Name - title.
     * @param string Value - can be set later.
     */
    function addTab($code, $name, $value = '')
    {
        $this -> tabs[$code] = $name;
        $this -> data[$code] = $value;  
    }
  
    /**
     * Sets data to a tab
     * @param string Code - ID - string without white space
     * @param string Value - can be set later.
     */
    function setData($code, $value)
    {
        $this -> data[$code] = $value;  
    }

    /**
     * Sets a default tab
     * @param string Code - ID - string without white space
     */
    function setDefault($code)
    {
        $this -> defaultTab = $code;
    }
  
    /**
     * Returns set of tabs as string
     * @return string
     */
    function show()
    {
        // if only one tab - show just the content of the one
        if(count($this->tabs) < 2)
        {
            return array_pop($this -> data); 
        }

        // JS and CSS
        \Cyfy\Cyfy ::  javaScript(\Cyfy\Cyfy ::  getSourcePath("Cyfy/Modules/Objects/tabs.js"), "file");
        \Cyfy\Cyfy ::  css(\Cyfy\Cyfy ::  getSourcePath("Cyfy/Modules/Objects/tabs.css"));
    
        $output .= "\n\n<div id=\"{$this -> unique}\">\n";
        $output .= "  <ul class=\"tabs-controller\">\n";
        // all the tabs
        $first = " class=\"first\"";
        foreach($this->tabs as $code => $name){
            $output .= "    <li class=\"tabs-tab\"><a href=\"#tab_{$code}\" id=\"con_{$code}\" onclick=\"showTab('{$code}', '{$this->unique}'); return false;\"{$first}>{$name}</a></li>\n";
            if($first !=""){
                $first = "";
            }
        }
        $output .= "  </ul>\n";
            
        // all the content
        $first = " first";
        $output .= "\n  <div class=\"tabs-all-data\">\n"
        ;
        foreach($this -> data as $code => $value){
            $output .= "    <div class=\"tabs-one-data{$first}\" id=\"tab_{$code}\">\n" . $value . "\n</div>\n";
            if($first !="")
            {
                $first = "";
            }
        }
        $output .= "  </div>\n";
        $output .= "</div>\n\n";
    
        $output .= \Cyfy\Cyfy ::  javaScript("	$(document).ready(function(){	  if('{$this -> defaultTab}'!=\"\"){ showTab('{$this -> defaultTab}', '{$this->unique}');	  }else{		showFirstTab('{$this->unique}');	  }  	});	");
    
    
        return $output;  
    }
}